<template>
  <div class="base-container">
    <t-form
      ref="form"
      :data="queryParams"
      label-width="80"
      @submit="
        () => {
          this.pagination.current = 1;
          this.queryParams.pageNum = 1;
          this.getList();
        }
      "
      @reset="
        () => {
          this.pagination.current = 1;
          this.queryParams.pageNum = 1;
          this.getList();
        }
      "
    >
      <t-row style="border-bottom: 1px solid #e5e6eb; margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="姓名" name="nickName">
            <t-input v-model="queryParams.nickName" clearable placeholder="请输入姓名" style="width: 240px" />
          </t-form-item>
          <t-form-item label="组别" name="recommendMajor">
            <t-select
              clearable
              style="width: 240px"
              :options="dict.type.recommend_major"
              placeholder="选择组别"
              v-model="queryParams.recommendMajor"
            >
            </t-select>
          </t-form-item>
          <t-form-item label="所在单位" name="departmentName">
            <!-- <t-tree-select style="width:240px"  clearable filterable :treeProps="{keys: {value: 'id'}}" v-model="queryParams.departmentId"
                           :data="deptOptions" placeholder="选择单位"></t-tree-select> -->
            <t-input v-model="queryParams.departmentName" clearable placeholder="请输入" style="width: 240px" />
          </t-form-item>
          <t-form-item label="手机号" name="phonenumber">
            <t-input v-model="queryParams.phonenumber" clearable placeholder="请输入" style="width: 240px" />
          </t-form-item>
          <t-form-item label="专业" name="majorName">
            <t-input v-model="queryParams.majorName" clearable placeholder="请输入" style="width: 240px" />
          </t-form-item>
          <t-form-item label="现从事专业" name="majorNowName">
            <t-input v-model="queryParams.majorNowName" clearable placeholder="请输入" style="width: 240px" />
          </t-form-item>
          <t-form-item label="职务" name="duty">
            <t-input v-model="queryParams.duty" clearable placeholder="请输入职务" style="width: 240px" />
          </t-form-item>
          <t-form-item label="职称" name="profTit">
            <t-select
              clearable
              style="width: 240px"
              :options="dictTypes.prof_tit"
              placeholder="选择职称"
              v-model="queryParams.profTit"
            >
            </t-select>
          </t-form-item>
          <t-form-item label="学历" name="education">
            <t-select
              clearable
              style="width: 240px"
              :options="dict.type.education"
              placeholder="选择学历"
              v-model="queryParams.education"
            >
            </t-select>
          </t-form-item>
          <t-form-item label="学位" name="degree">
            <t-select
              clearable
              style="width: 240px"
              :options="dict.type.degree"
              placeholder="选择学位"
              v-model="queryParams.degree"
            >
            </t-select>
          </t-form-item>
          <t-form-item label="来源" name="isInviteExpert">
            <t-select clearable style="width: 240px" v-model="queryParams.isInviteExpert">
              <t-option value="0" label="内部专家"></t-option>
              <t-option value="1" label="外部邀请"></t-option>
            </t-select>
          </t-form-item>
          <!--          <t-form-item label="状态" name="status">-->
          <!--            <t-select clearable style="width: 240px" placeholder="字典状态" v-model="queryParams.status">-->
          <!--              <t-option v-for="(item, index) in dict.type.sys_normal_disable" :value="item.value" :label="item.label" :key="index">-->
          <!--                {{ item.label }}-->
          <!--              </t-option>-->
          <!--            </t-select>-->
          <!--          </t-form-item>-->
          <!--          <t-form-item label="创建时间" name="status">-->
          <!--            <t-date-range-picker v-model="queryParams.dateRange" :placeholder="['开始时间','结束时间']" allow-input clearable/>-->
          <!--          </t-form-item>-->
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="handleAdd" variant="outline" theme="primary">
        <t-icon name="add" slot="icon" />
        新增
      </t-button>
      <t-button @click="exportExcel" variant="outline" theme="primary">
        <t-icon name="download" slot="icon" />
        导出
      </t-button>
    </div>
    <t-table
      key="table1"
      rowKey="index"
      :data="data"
      :columns="columns"
      hover
      :pagination="pagination"
      :loading="dataLoading"
    >
      <template #[`sysUser.nickName`]="{ row }">
        <t-link
          @click="
            dialog.objId = row.id;
            dialog.idCard = row.sysUser.idCard;
            dialog.header = row.sysUser.nickName;
            dialog.open_expert_detail = true;
            dialog.idCard = row.sysUser.idCard;
          "
          hover="color"
          theme="primary"
        >
          {{ row.sysUser.nickName }}<span v-if="row.isInviteExpert === '1'" style="color: red">(邀)</span>
        </t-link>
      </template>
      <template #[`sysUser.phonenumber`]="{ row }">
        {{ row.sysUser.phonenumber }}<span v-if="row.isJx !== '1'" style="color: red">(省外)</span>
      </template>
      <template #[`sysUser.duty`]="{ row }">
        <div v-if="['99999', ''].includes(row.sysUser?.profTit)">
          {{ row.sysUser?.duty }}
        </div>
        <div v-else>{{ row.sysUser?.duty }}/{{ row.sysUser?.profTit | filterByDict(dict.type.prof_tit) }}</div>
      </template>
      <template #recommendMajor="{ row }">
        {{ row.recommendMajor | filterByDict(dict.type.recommend_major) }}
      </template>
      <template #[`sysUser.education`]="{ row }">
        {{ row.sysUser?.education | filterByDict(dict.type.education) }}
      </template>
      <template #[`sysUser.degree`]="{ row }">
        {{ row.sysUser?.degree | filterByDict(dict.type.degree) }}
      </template>
      <template #isSediment="{ row }">
        <t-form>
          <!-- <t-form-item :labelWidth="60" labelAlign="left" name="isSediment" initialData="0">
            <template #label>
              <p style="font-size: 12px">是否沉淀:</p>
            </template>
            <t-switch v-model="row.isSediment" :customValue="['1', '0']" :onChange="value => onChangeSwitch(value, row)" />
          </t-form-item> -->
          <t-switch
            v-model="row.isSediment"
            :customValue="['1', '0']"
            :onChange="(value) => onChangeSwitch(value, row)"
          />
        </t-form>
      </template>
      <template #op="{ row }">
        <!-- <t-button size="small" @click="handleDetail(row)" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          编辑
        </t-button>
        <t-button
          theme="danger"
          @click="handleDel(row)"
          size="small"
          variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button> -->
        <!--        <t-button size="small" @click="dialog.header=row.sysUser.nickName;dialog.objId=row.id;dialog.open_all_detail=true"  variant="text" class="t-button-link">-->
        <!--          <t-icon name="info-circle" slot="icon"/>-->
        <!--          详情-->
        <!--        </t-button>-->
        <t-button
          size="small"
          @click="
            dialog.objId = row.id;
            dialog.idCard = row.sysUser.idCard;
            dialog.header = row.sysUser.nickName;
            dialog.open_expert_detail = true;
          "
          variant="text"
          class="t-button-link"
        >
          <t-icon name="info-circle" slot="icon" />
          评价信息
        </t-button>
      </template>
    </t-table>
    <expert-detail-dialog
      :header="dialog.header"
      :obj-id="dialog.objId"
      :id-card="dialog.idCard"
      :visible.sync="dialog.open_expert_detail"
      v-if="dialog.open_expert_detail"
    ></expert-detail-dialog>
  </div>
</template>

<script>
import { getDictList } from '@/api/system/dict/data';
// import * as XLSX from "xlsx";
// import FileSaver from "file-saver";
import ExpertDetailDialog from '../expert/components/expert-detail-dialog.vue';

export default {
  name: 'zjk-expert-expert-index',
  dicts: ['education', 'degree', 'prof_tit', 'recommend_major'],
  components: {
    // expertAllDetail,
    ExpertDetailDialog,
  },
  data() {
    return {
      dialog: {
        header: '',
        objId: null,
        open_all_detail: false,
        open_expert_detail: false,
        idCard: '',
      },
      dictTypes: {},
      deptOptions: [],
      currentRow: {},
      columns: [
        {
          colKey: 'sysUser.nickName',
          title: '专家姓名',
          align: 'center',
        },
        {
          colKey: 'age',
          title: '年龄',
          align: 'center',
        },
        {
          colKey: 'sysUser.phonenumber',
          title: '联系方式',
          align: 'center',
          width: 160,
        },
        {
          colKey: 'sysUser.dept.deptName',
          title: '所在单位',
          align: 'center',
        },
        {
          colKey: 'recommendMajor',
          title: '组别',
          align: 'center',
        },
        // {
        //   colKey: 'expertCategory',
        //   title: '专家类别',
        //   align: 'center',
        // },
        // {
        //   colKey: "category",
        //   title: "分类",
        //   align: "center"
        // },
        {
          colKey: 'sysUser.majorName',
          title: '专业',
          align: 'center',
        },
        {
          colKey: 'sysUser.majorNowName',
          title: '现从事专业',
          align: 'center',
        },
        {
          colKey: 'sysUser.duty',
          title: '职务/职称',
          align: 'center',
        },
        // {
        //   colKey: "profTit",
        //   title: "职称",
        //   align: "center"
        // },
        {
          colKey: 'sysUser.education',
          title: '学历',
          align: 'center',
        },
        {
          colKey: 'sysUser.degree',
          title: '学位',
          align: 'center',
        },
        // {
        //   colKey: "是否可用",
        //   title: "enable",
        //   align: "center"
        // },
        {
          colKey: 'isSediment',
          title: '是否',
          align: 'center',
          width: 60,
        },
        {
          colKey: 'op',
          title: '操作',
          align: 'center',
          width: 150,
        },
      ],
      data: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateRange: [],
        expertCategory: '',
        nickName: '',
        education: '',
        degree: '',
        classification: '',
        departmentId: null,
        recommendMajor: '',
        isInviteExpert: '',
        majorName: '',
        majorNowName: '',
      },
      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo);
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList();
        },
      },
      majorList: {},
    };
  },
  created() {
    Object.assign(this.queryParams, this.handelQuery(this.queryParams, this.$route.query));
    if (this.$route.meta.query) {
      Object.assign(this.queryParams, JSON.parse(this.$route.meta.query));
    }
    this.getList();
    this.getDeptTreeSelect();
    this.getDataTypes();
  },
  methods: {
    onChangeSwitch(value, row) {
      this.$api.zjk.expert
        .updateInfo({ id: row.id, isSediment: value })
        .then(this.$message.success('状态切换成功'))
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    // 定义导出Excel表格事件
    exportExcel() {
      this.$download(`/zjk/excel/expertsList`, this.queryParams, `专家列表_${new Date().getTime()}.xls`);
      // /* 从表生成工作簿对象 */
      // const wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));
      // /* 获取二进制字符串作为输出 */
      // const wbout = XLSX.write(wb, {
      //   bookType: "xlsx",
      //   bookSST: true,
      //   type: "array"
      // });
      // try {
      //   FileSaver.saveAs(
      //     // Blob 对象表示一个不可变、原始数据的类文件对象。
      //     // Blob 表示的不一定是JavaScript原生格式的数据。
      //     // File 接口基于Blob，继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
      //     // 返回一个新创建的 Blob 对象，其内容由参数中给定的数组串联组成。
      //     new Blob([wbout], { type: "application/octet-stream" }),
      //     // 设置导出文件名称
      //     `专家列表_${new Date().getTime()}.xls`
      //   );
      // } catch (e) {
      //   if (typeof console !== "undefined") console.log(e, wbout);
      // }
      // return wbout;
    },
    getDataTypes() {
      getDictList(['prof_tit'])
        .then((response) => {
          this.dictTypes = response.data;
          this.dictTypes.prof_tit = this.dictTypes.prof_tit.filter((val) => !['初级', '中级'].includes(val.label));
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    getDeptTreeSelect() {
      this.$api.system.dept
        .treeSelectAllIn()
        .then((response) => {
          this.deptOptions = response.data;
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    getList() {
      this.dataLoading = true;
      this.$api.zjk.expert
        .list(this.queryParams)
        .then((res) => {
          this.data = res.rows.map((row, index) => ({ ...row, index }));
          this.pagination.total = res.total;
          this.dataLoading = false;
        })
        .catch((e) => {
          this.dataLoading = false;
          this.$message.error(e.toString());
        });
    },
    handleDel(row) {
      const confirmDia = this.$dialog.confirm({
        header: '系统提示',
        body: `是否确认删专家名称为"${row.sysUser.nickName}"的数据项？`,
        theme: 'warning',
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          });
          this.$api.zjk.expert
            .del(row.id)
            .then((res) => {
              this.getList();
              this.$message.success('删除成功');
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            })
            .catch((e) => {
              confirmDia.update({
                confirmBtn: {
                  loading: false,
                },
              });
              this.$message.error(e.toString());
            });
        },
      });
    },
    handleDetail(row) {
      this.$router.push({
        path: `/zjk/other/expert/expert-detail/${row.id}`,
        query: { title: `${row.sysUser.nickName}-专家详情` },
      });
    },
    handleAdd() {
      this.$router.push(`/zjk/other/expert/expert-add/0`);
    },
  },
};
</script>

<style scoped></style>
